<? 
session_start();
				
include ('conn.php');
				
$db = get_conn();
				
check_session();

if ($_POST['delete_selected']!=""){
	
	$id_invoice = $_POST['id_invoice'];
	if(is_array($id_invoice)){
		$regs_to_delete =count($id_invoice);
		for($i=0; $i < count($id_invoice); $i++){

			$sql = "delete from invoices where id_invoice =".$id_invoice[$i];
			$result = mysql_query($sql);
			if(!$result){
				error_handling('invoices.insert  - '.$sql,mysql_error());
			}
		}	
	}else{
		if($id_invoice !=''){
			$regs_to_delete =1;
			$sql = "delete from invoices id_invoice=".$id_invoice;
			$result = mysql_query($sql);
			if(!$result){
				error_handling('invoices.insert  - '.$sql,mysql_error());
			}			
		}
	}
	
	if($_POST['cantidad'] > $regs_to_delete){
		do_redirect('../invoices_step2.php'); 	  
	}else{
		do_redirect('../misc.php?id=2'); 	  
	}
		 
}//Agregar


		  
if ($_POST['create_invoices']!=""){
	
	$id_location = $_POST['id_location'];
	$amount = get_type_amount($_POST['id_it']);
	if(is_array($id_location)){
		for($i=0; $i < count($id_location); $i++){

			$sql = "INSERT INTO invoices (id_location,id_it,amount,istatus,due_date,exp_date) VALUES (
					".$id_location[$i].",
					".($_POST['id_it']).",
					".$amount.",
					'W',
					'".un_format_date($_POST['due_date'])."',
					'".un_format_date($_POST['exp_date'])."')";
			$result = mysql_query($sql);
			if(!$result){
				error_handling('invoices.insert  - '.$sql,mysql_error());
			}
		}	
	}else{
		if($id_location !=''){
			$sql = "INSERT INTO invoices (id_location,id_it,amount,istatus,due_date,exp_date) VALUES (
					".$id_location.",
					".($_POST['id_it']).",
					".$amount.",
					'W',
					'".un_format_date($_POST['due_date'])."',
					'".un_format_date($_POST['exp_date'])."')";

			$result = mysql_query($sql);
			if(!$result){
				error_handling('invoices.insert  - '.$sql,mysql_error());
			}			
		}
	}
	do_redirect('../invoices_step2.php'); 	  
		 
}//Agregar
		  
if ($_POST['Actualizar']!=""){
			
	$sql = "update invoices set 
	id_it=".$_POST['id_it'].",
	amount=".$_POST['amount'].",
	due_date=\"".($_POST['due_date'])."\",
	exp_date=\"".($_POST['exp_date'])."\",
	description=\"".htmlspecialchars($_POST['description'])."\"
 
	where id_invoice=".$_POST['id_invoice'];
			 
			
	$result = mysql_query($sql);
			
	if($result){
				
	echo "
		<html>
		<head> 
		<script src='../tools/modal/jquery-1.4.2.js'     type='text/javascript'></script>
		<script type='text/javascript'>
		function callback(){
			parent.refresh_list();
			parent.$.modal().close();
		}
		</script>
		</head>
		<body onLoad='callback();'></body>
		</html>";		  
						
			
	}else{
				
		error_handling('invoices.update - '.$sql,mysql_error());
			
	}
		 
}//update
		  
		  
if ($_POST['report_payment']!=""){
	$sql = "INSERT INTO payments (id_location,id_invoice,pdate,amount,description) VALUES (
	".($_POST['id_location']).",
	".($_POST['id_invoice']).",
	'".date("Y-h-d")."',
	".($_POST['amount']).",
	\"".htmlspecialchars($_POST['description'])."\")";
			
	$result = mysql_query($sql);
 	if($result){
		$id_payment = mysql_insert_id();
		
		update_invoice_status($_POST['id_invoice'],'C');
		add_receive($id_payment,$_FILES['recibo']);		
	
	/*
	echo "
		<html>
		<head> 
		<script src='../tools/modal/jquery-1.4.2.js'     type='text/javascript'></script>
		<script type='text/javascript'>
		function callback(){
			parent.refresh_list();
			parent.$.modal().close();
		}
		</script>
		</head>
		<body onLoad='callback();'></body>
		</html>";
	*/	
	do_redirect('../misc.php?id=3s');	  
		
 	}else{
 		error_handling('payments.insert  - '.$sql,mysql_error());
 	}
}



if ($_POST['finish']!=""){
	$id_invoice = $_POST['id_invoice'];

	if(is_array($id_invoice)){
		for($i=0; $i < count($id_invoice); $i++){
			$sql = "update invoices set istatus='P' where id_invoice=".$id_invoice[$i];
			$result = mysql_query($sql);
			if(!$result){
				error_handling('invoices.update a  - '.$sql,mysql_error());
			}else{
				//send_invoice_by_email($id_invoice[$i]);
			}
		}	
	}else{
		if($id_invoice !=''){
			$sql = "update invoices set istatus='P' where id_invoice=".$id_invoice;
			$result = mysql_query($sql);
			if(!$result){
				error_handling('invoices.update b  - '.$sql,mysql_error());
			}else{			
				//send_invoice_by_email($id_invoice);			
			}
		}
	}
	do_redirect('../misc.php?id=1');	  
}
		  
		  
if ($_POST['Eliminar']!=""){
	$sql = "delete from invoices_type where id_it=".$_POST['id_it'];
	$result = mysql_query($sql);
	if($result){
		do_redirect('../invoices_types.php'); 	  
	}else{
		error_handling('invoices_type.delete - '.$sql,mysql_error());
	}				  
}//delete
		  
if ($_POST['Nuevo']!=""){
				
		do_redirect('../invoices_types.php'); 	  
		  
}//nuevo


if ($_GET['act']=="rp" && $_GET['id']!="" && $_GET['id_invoice']!=""){
	$sql = "update invoices set istatus='T' where id_invoice=".$_GET['id_invoice'];
	$result = mysql_query($sql);
	if(!$result){
		error_handling('invoices_type.delete - '.$sql,mysql_error());
	}
	do_redirect('../receive_payments.php'); 	  
}//nuevo


function update_invoice_status($id_invoice,$status){
	if ($id_invoice !="" && $status !=""){
				
		$sql = "update invoices set istatus=\"".$status."\"	where id_invoice=".$id_invoice;
		$result = mysql_query($sql);
		if(!$result){
			error_handling('invoices.update - '.$sql,mysql_error());
		}				  
	}//update	
}



function add_receive($id_payment,$file){

	if($file['name']!=''){
		$destination = "../file_storage/recibos"; 
		$file_parts = explode(".",$file['name']);
		
		$file_name = $id_payment.".".$file_parts[count($file_parts)-1];
		$path = $destination."/".$file_name;
		
		
		$funciono = move_uploaded_file($file['tmp_name'], $path);
		if($funciono){
			$sql = "update payments set	receive_file=\"".$file_name."\"	where id_payment=".$id_payment;
			$result = mysql_query($sql);
			if(!$result){
				error_handling('payments.update - '.$sql,mysql_error());
			}			
			
			return $file_name;
		}else{
			echo "Error en el proceso de carga, intente de nuevo"; exit;
		}
	}//image 
}//upload


function send_invoice_by_email($id_invoice){
	
	if($id_invoice !=''){
		$to = "danramza@gmail.com";
	
	
	
		$body=  $to." <br><br>Executed:<br>".$sql." <br><br>at ".date("l dS of F Y h:i:s A").
		"<br><br>Error in File: <br>".$_SERVER['PHP_SELF'].
		"<br><br>Request Vars<br>".$variables.
		"<br><br>Session Vars<br><br>".$session_vars;
	
	
		$headers .= "From:".$_SERVER['SERVER_NAME']."<info@danramza.com>\n";
		$headers .= "X-Sender: <info@danramza.com>\n";
		$headers .= "X-Mailer: PHP\n"; //mailer
		$headers .= "X-Priority: 3\n"; //1 UrgentMessage, 3 Normal
		$headers .= "Return-Path: <info@danramza.com>>\n";
		$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
	
		$x =mail($to, $head , $body ,$headers);
		
	}	
}
?>